package cn.ffcs.uec.organization.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.organization.dao.UecOrgOrgsDao;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecOrgOrgsDao")
public class UecOrgOrgsDaoImpl extends BaseDaoImpl implements UecOrgOrgsDao {

	@Override
	public UecOrgOrgs findUecOrgOrgsByOrganizationRelationVo(
			OrganizationRelationVo organizationRelationVo) {
		if (null != organizationRelationVo) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ORG_ORGS T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (null != organizationRelationVo.getOrg()) {
				sb.append(" AND T.PK_ORG = ? ");
				params.add(organizationRelationVo.getOrg());
			}
			UecOrgOrgs uecOrgOrgs = super.jdbcFindObject(sb.toString(), params,
					UecOrgOrgs.class);
			return uecOrgOrgs;
		}
		return null;
	}

	@Override
	public UecOrgOrgs findUecOrgOrgsByUecOrgOrgs(
			UecOrgOrgs uecOrgOrgs) {
		if(null != uecOrgOrgs.getPkOrg()){
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ORG_ORGS T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (null != uecOrgOrgs.getPkOrg()) {
				sb.append(" AND T.PK_ORG = ? ");
				params.add(uecOrgOrgs.getPkOrg());
			}
			List<UecOrgOrgs> list = super.jdbcFindList(sb.toString(), params,
					UecOrgOrgs.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}
	
	@Override
	public PageInfo queryOrganization(UecOrgOrgs uecOrgOrgs, int currentPage, int pageSize) {
		//String orgName = PermissionUtil.getManagerOrg();
		String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
		String orgName ="";
		//String[] orgnamelist= {"呼伦","兴安","通辽","赤峰","锡林","乌兰","乌海"};
		String[] orgnamelist=null;
		String orgNametemp =null;
		if(!StrUtil.isNullOrEmpty(code)) {
			orgNametemp = PermissionUtil.getManagerOrg();
			if(!StrUtil.isNullOrEmpty(orgNametemp)) {
				orgnamelist=orgNametemp.split(",");
			}
			if(orgnamelist != null && orgnamelist.length==1) {
				orgName=orgnamelist[0];
			}
		}
		StringBuilder sql = new StringBuilder(" SELECT * FROM  UEC_ORG_ORGS v WHERE  V.STATUS_CD = ? AND V.enablestate = '2' ");
		List<Object> params = new ArrayList<Object>();
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		if (null != uecOrgOrgs) {
			if (!StrUtil.isNullOrEmpty(uecOrgOrgs.getPkFatherorg())) {
				
				if("0001A11000000003UHTY".equals(uecOrgOrgs.getPkFatherorg())) {
					sql.append(" AND (V.pk_fatherorg = ? ");
					params.add(uecOrgOrgs.getPkFatherorg());
					sql.append(" OR V.PK_ORG = ? )");
					params.add(uecOrgOrgs.getPkFatherorg());
				}else {
					sql.append(" AND V.pk_fatherorg = ? ");
					params.add(uecOrgOrgs.getPkFatherorg());
				}
			}
			if (!StrUtil.isNullOrEmpty(uecOrgOrgs.getCode())) {
				sql.append(" AND V.code = ? ");
				params.add(uecOrgOrgs.getCode());
			}
			if (!StrUtil.isNullOrEmpty(uecOrgOrgs.getName())) {
				sql.append(" AND V.Name like ? ");
				params.add("%" + uecOrgOrgs.getName() + "%");
			}
			if (!"区本部".equals(orgName)) {
				sql.append(" AND V.Name LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
			}
			if(orgnamelist !=null && orgnamelist.length>1) {
				sql.append("AND (V.Name like ? ");
				params.add("%"+orgnamelist[0]+"%");
				for(int i=1;i<orgnamelist.length-1;i++) {
					sql.append(" OR V.Name like ? ");
					params.add("%"+orgnamelist[i]+"%");
				}
				sql.append(" OR V.Name like ? )");
				params.add("%"+orgnamelist[orgnamelist.length-1]+"%");
			}
			sql.append(" ORDER BY V.name,V.code");
			return super.jdbcFindPageInfo(sql.toString(), params, currentPage, pageSize, UecOrgOrgs.class);
		}
		return null;
	}

	@Override
	public String getUecOrgOrgsNameByPkOrg(String pkOrg) {
		if(!StrUtil.isEmpty(pkOrg)) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ORG_ORGS T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			sb.append(" AND T.PK_ORG = ? ");
			params.add(pkOrg);
			List<UecOrgOrgs> list = super.jdbcFindList(sb.toString(), params,
					UecOrgOrgs.class);
			if (list.size() > 0 && list != null) {
				return list.get(0).getName();
			}
		}
		return "";
	}

	@Override
	public PageInfo queryNextUecOrgOrgsByUecOrgOrgs(UecOrgOrgs uecOrgOrgs, int currentPage, int pageSize) {
		if (null != uecOrgOrgs) {
			if (!StrUtil.isEmpty(uecOrgOrgs.getPkFatherorg())) {
				StringBuilder sb = new StringBuilder(" SELECT * FROM  UEC_ORG_ORGS T START WITH T.pk_fatherorg = ? "
						+ " CONNECT BY PRIOR T.pk_org = T.pk_fatherorg AND T.Status_Cd = ? ");
				List<Object> params = new ArrayList<Object>();
				params.add(uecOrgOrgs.getPkFatherorg());
				params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
				return super.jdbcFindPageInfo(sb.toString(), params, currentPage, pageSize, UecOrgOrgs.class);
			}
		}
		return null;
	}

	@Override
	public String getUecOrgOrgsPreOrgByPkOrg(String pkOrg) {
		if(!StrUtil.isEmpty(pkOrg)) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ORG_ORGS T WHERE T.STATUS_CD = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			sb.append(" AND T.PK_ORG = ? ");
			params.add(pkOrg);
			List<UecOrgOrgs> list = super.jdbcFindList(sb.toString(), params,
					UecOrgOrgs.class);
			if (list.size() > 0 && list != null) {
				return list.get(0).getPkFatherorg();
			}
		}
		return "";
	}
	@Override
	public String getOrgRootByName(String orgName) {
		if (orgName != null) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM   VIEW_ORGANIZATION_RELATION T WHERE T.LV='2' AND T.ORGNAME like ? ");
			List<Object> params = new ArrayList<Object>();
			params.add("'%'"+orgName+"'%'");
			List<OrganizationRelationVo> list = OrganizationRelationVo
					.repository().jdbcFindList(sb.toString(), params,
							OrganizationRelationVo.class);
			return list.get(0).getOrg();
		}
		return null;
	}
}
